package com.design3.graduatethesissys.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.design3.graduatethesissys.common.utils.FileUtil;
import com.design3.graduatethesissys.common.utils.GetUserName;
import com.design3.graduatethesissys.domain.PingJia;
import com.design3.graduatethesissys.domain.Teacher;
import com.design3.graduatethesissys.mapper.ChuqiMapper;
import com.design3.graduatethesissys.mapper.StudentMapper;
import com.design3.graduatethesissys.mapper.TeacherMapper;
import com.design3.graduatethesissys.service.TeacherService;

@Service
public class TeacherServiceImpl implements TeacherService {

	@Autowired
	TeacherMapper teacherMapper;
	
	@Autowired
	ChuqiMapper chuqiMapper;
	
	@Override
	public List<Map<String, Object>> selectTeacher() {
		String teh_id = GetUserName.getUserName();
		return teacherMapper.selectTeacher(teh_id);
	}

	@Override
	public Map<String, Object> updateTeacher(Teacher teacher) {
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			teacherMapper.updateTeacher(teacher);
			result.put("code", "200");
			return result;
		}catch (Exception e) {
			result.put("code", "500");
			return result;
		}
	}

	@Override
	public List<Map<String, Object>> selectCqStudent(String teh_id) {
		List<Map<String, Object>> list = teacherMapper.selectStudent(teh_id);
		int length = list.size();
		for( int i = 0;i<length;i++) {
			if("st02".equals(list.get(i).get("stu_status"))) {
				list.get(i).put("stu_status", "待审核");
			}else {
				list.get(i).put("stu_status", "已评审");
			}
			if("true".equals(FileUtil.isUpload((String)list.get(i).get("stu_id"), "pj01"))){
				list.get(i).put("isupload", "true");
			}else {
				list.get(i).put("isupload", "false");
			}
		}
		return list;
	}

	@Override
	public List<Map<String, Object>> selectZqStudent(String teh_id) {
		List<Map<String, Object>> list = teacherMapper.selectStudent(teh_id);
		int length = list.size();
		for( int i = 0;i<length;i++) {
			if("st03".equals(list.get(i).get("stu_status"))) {
				list.get(i).put("stu_status", "待审核");
			}else if("st02".equals(list.get(i).get("stu_status"))){
				list.get(i).put("stu_status", "待提交");
			}else {
				list.get(i).put("stu_status", "已评审");
			}
			if("true".equals(FileUtil.isUpload((String)list.get(i).get("stu_id"), "pj02"))){
				list.get(i).put("isupload", "true");
			}else {
				list.get(i).put("isupload", "false");
			}
		}
		return list;
	}

	@Override
	public List<Map<String, Object>> selectZzStudent(String teh_id) {
		List<Map<String, Object>> list = teacherMapper.selectStudent(teh_id);
		int length = list.size();
		for( int i = 0;i<length;i++) {
			if("st04".equals(list.get(i).get("stu_status"))) {
				list.get(i).put("stu_status", "待审核");
			}else if("st03".equals(list.get(i).get("stu_status"))) {
				list.get(i).put("stu_status", "待提交");
			}else if("st02".equals(list.get(i).get("stu_status"))) {
				list.get(i).put("stu_status", "待提交");
			}else {
				list.get(i).put("stu_status", "已评审");
			}
			if("true".equals(FileUtil.isUpload((String)list.get(i).get("stu_id"), "pj03"))){
				list.get(i).put("isupload", "true");
			}else {
				list.get(i).put("isupload", "false");
			}
		}
		return list;
	}

	@Override
	public List<Map<String, Object>> selectAllStudent(String teh_id) {
		return teacherMapper.selectMesCount(teh_id);
	}

	@Override
	public List<Map<String, Object>> selectAllSbj(String teh_id) {
		return teacherMapper.selectAllSbj(teh_id);
	}

	@Override
	public List<PingJia> getPingjia(String stu_id,String pj_status) {
		return chuqiMapper.pingJiList(stu_id, pj_status);
	}

	@Override
	public Map<String, String> uploadSubject(MultipartFile file, String sbj_id) {
		Map<String, String> map = new HashMap<String, String>();
		String teh_id = GetUserName.getUserName();
		String result = FileUtil.upload(file, teh_id, sbj_id);
		map.put("isupload", result);
		return map;
	}
}
